package com.itheima.c3p0;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class C3p0Test01 {
    public static void main(String[] args) {
        //1.创建c3p0的数据库连接池对象
        DataSource dataSource = new ComboPooledDataSource();
        try {
            //2.通过连接池对象获取数据库连接
            Connection con = dataSource.getConnection();
            //3.执行操作
            String sql = "select * from student";
            PreparedStatement ps = con.prepareStatement(sql);
            //4.执行sql语句，接收结果集
            ResultSet res = ps.executeQuery();
            while(res.next()){
                System.out.println(res.getInt("sid") + "\t" + res.getString("name") + "\t" + res.getInt("age") + "\t" + res.getDate("birthday"));
            }
            //6.释放资源
            con.close();
            ps.close();
            res.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
